package com.example.kwic.Controller;

import com.example.kwic.Main_Branch.demo1;
import com.example.kwic.OOB.Alphabetizer;
import com.example.kwic.OOB.Input;
import com.example.kwic.OOB.Output;
import com.example.kwic.OOB.Shift;
import com.example.kwic.Watcher.KWICSubject;
import org.apache.commons.io.FileUtils;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class Controller {
    @GetMapping("/oob")
    public List<String> OOB() throws IOException {
        List<String> list =  new ArrayList<>();
        Input input = new Input();
        input.input("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\input.txt");
        Shift shift = new Shift(input.getLineTxt());
        shift.shift();
        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());
        alphabetizer.sort();
        Output output = new Output(alphabetizer.getKwicList());
        output.output("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\output.txt");
        try {
            list = FileUtils.readLines(new File("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\output.txt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @GetMapping("/mabr")
    public List<String> Main_Branch() throws IOException {
        List<String> list =  new ArrayList<>();
        demo1 kwic = new demo1();
        kwic.input("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\input.txt");
        kwic.shift();
        kwic.alphabetizer();
        kwic.output("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\output.txt");
        try {
            list = FileUtils.readLines(new File("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\output.txt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @GetMapping("/pipe")
    public List<String> pipe() throws IOException {
        List<String> list =  new ArrayList<>();

        try {
            list = FileUtils.readLines(new File("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\output.txt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  list;
    }

    @GetMapping("/observer")
    public List<String>  Oberserve(){
        List<String> list =  new ArrayList<>();
        KWICSubject kwicSubject = new KWICSubject();
        //创建观察者
        com.example.kwic.Watcher.Input input = new com.example.kwic.Watcher.Input("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\input.txt");
        com.example.kwic.Watcher.Shift shift = new com.example.kwic.Watcher.Shift(input.getLineTxt());
        com.example.kwic.Watcher.Alphabetizer alphabetizer = new com.example.kwic.Watcher.Alphabetizer(shift.getKwicList());
        com.example.kwic.Watcher.Output output = new com.example.kwic.Watcher.Output(alphabetizer.getKwicList(), "E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\output.txt");

        // 将观察者加入主题
        kwicSubject.addObserver(input);
        kwicSubject.addObserver(shift);
        kwicSubject.addObserver(alphabetizer);
        kwicSubject.addObserver(output);
        // 逐步调用各个观察者
        kwicSubject.startKWIC();
        try {
            list = FileUtils.readLines(new File("E:\\IDEAProject\\KWICSearch\\src\\main\\resources\\output.txt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  list;
    }
    
    
    
}
